LEFT = 0
HEIGHT = 1
RIGHT = 2

def skyline(buildings):

	if len(buildings) == 1:
		keypoints= []
		keypoints.append((buildings[0][LEFT],buildings[0][HEIGHT]))
		keypoints.append((buildings[0][RIGHT],0))
		return keypoints

	keypoints = []
	ListA = skyline(buildings[(len(buildings)/2):])
	ListB = skyline(buildings[:(len(buildings)/2)])
	
	CurrentHeightA = 0
	CurrentHeightB = 0
	CurrentX = 0

	while ListA and ListB:
		
		elemA = ListA[0]
		elemB = ListB[0]

		if  elemA[LEFT] < elemB[LEFT]:
			CurrentHeightA = elemA[HEIGHT]
			CurrentX = elemA[LEFT]
			keypoints.append((CurrentX, max(CurrentHeightA,CurrentHeightB)))	
			del ListA[0]
		else:
			CurrentX = elemB[LEFT]
			CurrentHeightB = elemB[HEIGHT]
			keypoints.append((CurrentX, max(CurrentHeightA,CurrentHeightB)))
			del ListB[0]

	if not ListA:
		while ListB:
			elemB = ListB[0]
			keypoints.append(elemB)
			del ListB[0];
			
	if not ListB:
		while ListA:
			elemA = ListA[0]
			keypoints.append(elemA)
			del ListA[0];
	return keypoints

def mergeBuildings(buildings):
	actualHeight = 0
	builds = []
	for i in buildings:
		if i[HEIGHT] != actualHeight:
			builds.append(i)
			actualHeight = i[HEIGHT]
	return builds



if __name__ == '__main__':
	buildingsFile = raw_input("Enter the file name containing a valid graph:")
	filep = open(buildingsFile)
	
	text=filep.read()
	#Splitting into the (L,H,R) building triples or L-terns
	buildings = text.strip('\n').split('\n')
	#Splitting each triple to a list of 3 values and casting them to integers
	buildings = [map(int,x.split(',')) for x in buildings]
	print "\n"
	print mergeBuildings(skyline(buildings))
	